#!/usr/bin/python2.7
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

'''
Created on Dec 7, 2011

@author: ibrahimelfar
'''

from prototype.apps import eclipse
from prototype.automation import Cut, Enter, Key, Paste, Text, Up
from prototype.core import Activity, master_registry, Registry
from prototype.apps.eclipse import Eclipse
from prototype.apps.notepad import Notepad

class HtmlTemplate(eclipse.Template):
    def __init__(self, name, content):
        eclipse.Template.__init__(self, language = 'html', context = 'html_all', name = name, content = content)

registry = Registry()
master_registry.register('html', registry)

all_tags = ['!DOCTYPE',
            'a',
            'abbr',
            'acronym',
            'address',
            'applet',
            'area',
            'b',
            'base',
            'basefont',
            'bdo',
            'big',
            'blockquote',
            'body',
            'br',
            'button',
            'caption',
            'center',
            'cite',
            'code',
            'col',
            'colgroup',
            'dd',
            'del',
            'dfn',
            'dir',
            'div',
            'dl',
            'dt',
            'em',
            'fieldset',
            'font',
            'form',
            'frame',
            'frameset',
            'head',
            'h1',
            'h2',
            'h3',
            'h4',
            'h5',
            'h6',
            'hr',
            'html',
            'i',
            'iframe',
            'img',
            'input',
            'ins',
            'kbd',
            'label',
            'legend',
            'li',
            'link',
            'map',
            'menu',
            'meta',
            'noframes',
            'noscript',
            'object',
            'ol',
            'optgroup',
            'option',
            'p',
            'param',
            'pre',
            'q',
            's',
            'samp',
            'script',
            'select',
            'small',
            'span',
            'strike',
            'strong',
            'style',
            'sub',
            'sup',
            'table',
            'tbody',
            'td',
            'textarea',
            'tfoot',
            'th',
            'thead',
            'title',
            'tr',
            'tt',
            'u',
            'ul',
            'var',
            ]

standalone_tags = [
            'br',
            'hr',
            ]

multiple_line_tags = [
            'address',
            'applet',
            'area',
            'blockquote',
            'body',
            'cite',
            'code',
            'col',
            'colgroup',
            'dd',
            'dir',
            'div',
            'dl',
            'dt',
            'fieldset',
            'form',
            'frame',
            'frameset',
            'head',
            'html',
            'iframe',
            'legend',
            'map',
            'menu',
            'ol',
            'optgroup',
            'p',
            'pre',
            'samp',
            'script',
            'span',
            'style',
            'table',
            'tbody',
            'tfoot',
            'th',
            'thead',
            'tr',
            'tt',
                      ]

single_line_tags = [t for t in all_tags if not t in multiple_line_tags + standalone_tags]

spoken_form = dict([(x, x) for x in all_tags])
spoken_form.update(
{'!DOCTYPE': 'document type',
            'a': 'hyperlink',
            'abbr': 'abbreviation',
            'b': 'bold',
            'basefont': 'base font',
            'bdo': 'bidirectinal override',
            'blockquote': 'block quote',
            'br': 'line break',
            'col': 'column',
            'colgroup': 'column group',
            'dd': 'data definition',
            'del': 'deleted',
            'dfn': 'definition',
            'dir': 'directory',
            'div': 'division',
            'dl': 'definition list',
            'dt': 'data type',
            'em': 'emphasis',
            'fieldset': 'field set',
            'frameset': 'frame set',
            'h1': 'heading one',
            'h2': 'heading two',
            'h3': 'heading three',
            'h4': 'heading four',
            'h5': 'heading five',
            'h6': 'heading six',
            'hr': 'horizontal rule',
            'html': 'HTML',
            'i': 'italic',
            'iframe': 'I frame',
            'img': 'image',
            'ins': 'deleted',
            'kbd': 'keyboard',
            'li': 'list item',
            'noframes': 'no frames',
            'noscript': 'no script',
            'ol': 'ordered list',
            'optgroup': 'option group',
            'p': 'paragraph',
            'param': 'parameter',
            'pre': 'preformatted',
            'q': 'short quotation',
            's': 'strikethrough',
            'samp': 'sample',
            'sub': 'subscript',
            'sup': 'superscript',
            'tbody': 'table body',
            'td': 'table cell',
            'textarea': 'text area',
            'tfoot': 'table footer',
            'th': 'table header',
            'tr': 'table row',
            'tt': 'teletype',
            'u': 'underline',
            'ul': 'unordered list',
            'var': 'variable',
            })

utterance_lists = dict(
                       create_new=['create', 'new', 'add', 'insert'],
                       mark_tag=['mark', 'tag'],
                       mark=['mark', 'mark as', 'mark that as'],
                       spoken_tags = spoken_form.values() + ['tag']
                       )

activity_pattern_variables = dict(
                                  single_line_tag=['b', 'i'],
                                  multiple_line_tag=['head', 'body'],
    
                                   spoken=dict(abbr='abbreviation',
                                               b='bold',
                                               i='italic',
                                               u='underline',
                                               )
                                  )

vocabulary = dict(
                  create_tag = {'en_us': ['[create_new] [spoken_tags]']},
                  tag_that = {'en-us': ['[mark] [spoken_tags]']}
                  )

templates = dict(
                create_tag = [
                              Pattern('create-{{spoken_tag}}',
                                      '<{{tag}}>${cursor}</{{tag}}>',
                                     spoken = utterance_lists.spoken_tags,
                                     literal = all_tags),
                              Pattern('create-{{spoken_tag}}',
                                     '<{{tag}}>{{break}}${cursor}{{break}}</{{tag}}>',
                                     spoken = utterance_lists.spoken_tags,
                                     literal = all_tags),
                              Pattern('create-{{spoken_tag}}',
                                     '<{{tag}}${cursor}/>',
                                     spoken = utterance_lists.spoken_tags,
                                     literal = all_tags),
                              Pattern('create-tag',
                                     '<${tag}>${cursor}</${tag}>',
                                     spoken = utterance_lists.spoken_tags,
                                     literal = all_tags)
                              ]
                )

default_eclipse_context = EclipseContext(language, context)
eclipse = dict(
               create_tag = mycontext,
               tag_that = ...
               )

#registry.register_context({'variables': variables})

#Activity(
#         registry,
#         name = 'CreateTag',
#         utterances={'en_us': ['[create_new] tag']},
#         apps=dict(
#                   notepad=Notepad(steps=[Text('<[tag]></[tag]>')]
#                                   ),
#                   eclipse=Eclipse(template = HtmlTemplate(name='create-tag',
#                                                           content='<${tag}>${cursor}</${tag}>'),
#                                   )                 
#                   ))
#
#
#registry.register(Activity(
#                           name = 'CreateTag',
#                           utterances={'en_us': ['[create_new] tag']},
#                           apps=dict(
#                                     notepad=Notepad(steps=[Text('<[tag]></[tag]>')]
#                                                     ),
#                                     eclipse=Eclipse(template = HtmlTemplate(name='create-tag',
#                                                                             content='<${tag}>${cursor}</${tag}>'),
#                                                     steps=['serious']
#                                                     )                 
#                                     )
#                           ))
                  
#class CreateTag(Activity):
#    utterances = {'en_us': ['[create_new] tag']}
#
#    apps = dict(
#                notepad=Notepad(steps=[Text('<[tag]></[tag]>')]
#                                  ),
#
#                eclipse=Eclipse(HtmlTemplate(name='create-tag',
#                                             content='<${tag}>${cursor}</${tag}>'))
#                )

#class TagThat(Activity):
#    utterances = {'en-us': ['[mark_tag] that']}
#    
#    apps = dict(
#                notepad=Notepad(steps=[Cut,
#                                       Text('<[tag]>'),
#                                       Paste,
#                                       Text('</[tag]>'),
#                                       ]),
#
#                eclipse=Eclipse(HtmlTemplate(name='create-tag',
#                                             content='<${tag}>${text}</${tag}>${cursor}'), 1)
#                )
#registry.register(TagThat)
#
#class SingleLineTag(Activity):
#    utterances = {'en-us': ['[create_new] {{spoken[tag]}}']}
#    pattern_variables = {'tag': variables['single_line_tag']}
#    
#    apps = dict(
#                notepad=Notepad(steps=[Text('<{{tag}}></{{tag}}>'),
#                                       Key('Left', 5),
#                                       ]),
#
#                eclipse=Eclipse(HtmlTemplate(name='create-{{tag}}',
#                                             content='<{{tag}}>${cursor}</{{tag}}>'))
#                )
#registry.register(SingleLineTag)
#
#class SingleLineTagThat(Activity):
#    utterances = {'en-us': ['[mark] {{spoken[tag]}}', '{{spoken[tag]}} that']}
#    pattern_variables = {'tag': variables['single_line_tag']}
#    
#    apps = dict(
#                notepad=Notepad(steps=[Cut,
#                                       Text('<{{tag}}>'),
#                                       Paste,
#                                       Text('</{{tag}}>'),
#                                       Key('Left', 5),
#                                       ]),
#
#                eclipse=Eclipse(HtmlTemplate(name='create-{{tag}}',
#                                             content='<{{tag}}>${text}</{{tag}}${cursor}>'), 1)
#                )
#registry.register(SingleLineTagThat)
#
#class MultipleLineTag(Activity):
#    utterances = {'en-us': ['[create_new] {{spoken[tag]}}']}
#    pattern_variables = {'tag': variables['multiple_line_tag']}
#    
#    apps = dict(
#                notepad=Notepad(steps=[Text('<{{tag}}></{{tag}}>'),
#                                       Key('Left', 5),
#                                       ]),
#
#                eclipse=Eclipse(HtmlTemplate(name='create-{{tag}}',
#                                             content='<{{tag}}>${cursor}</{{tag}}>'))
#                )
#registry.register(MultipleLineTag)
#
#class MultipleLineTagThat(Activity):
#    utterances = {'en-us': ['[mark] {{spoken[tag]}}', '{{spoken[tag]}} that']}
#    pattern_variables = {'tag': variables['multiple_line_tag']}
#    
#    apps = dict(
#                notepad=Notepad(steps=[Cut,
#                                       Text('<{{tag}}>'),
#                                       Enter,
#                                       Paste,
#                                       Text('</{{tag}}>'),
#                                       Enter,
#                                       Up(2)
#                                       ]),
#
#                eclipse=Eclipse(HtmlTemplate(name='create-{{tag}}',
#                                             content='<{{tag}}>${text}</{{tag}}${cursor}>'), 1)
#                )
#registry.register(MultipleLineTagThat)
